﻿CREATE PROCEDURE [dbo].[yaf_board_delete](
                @BoardID INT)
AS
    BEGIN
        DECLARE  @tmpForumID INT;
        DECLARE forum_cursor CURSOR  FOR
        SELECT   ForumID
        FROM     yaf_Forum a
                 JOIN yaf_Category b
                   ON a.CategoryID = b.CategoryID
        WHERE    b.BoardID = @BoardID
        ORDER BY ForumID DESC
        OPEN forum_cursor
        FETCH NEXT FROM forum_cursor
        INTO @tmpForumID
        WHILE @@FETCH_STATUS = 0
        BEGIN
            EXEC yaf_forum_delete
                 @tmpForumID;
            FETCH NEXT FROM forum_cursor
            INTO @tmpForumID
        END
        CLOSE forum_cursor
        DEALLOCATE forum_cursor
        DELETE FROM yaf_ForumAccess
        WHERE       EXISTS (SELECT 1
                FROM   yaf_Group x
                WHERE  x.GroupID = yaf_ForumAccess.GroupID
                AND x.BoardID = @BoardID)
        DELETE FROM yaf_Forum
        WHERE       EXISTS (SELECT 1
                FROM   yaf_Category x
                WHERE  x.CategoryID = yaf_Forum.CategoryID
                AND x.BoardID = @BoardID)
                
		ALTER TABLE yaf_UserGroup DISABLE TRIGGER ALL

        DELETE FROM yaf_UserGroup
        WHERE       EXISTS (SELECT 1
                FROM   yaf_User x
                WHERE  x.UserID = yaf_UserGroup.UserID
                AND x.BoardID = @BoardID)

		ALTER TABLE yaf_UserGroup ENABLE TRIGGER ALL
                
        DELETE FROM yaf_Category
        WHERE       BoardID = @BoardID
        DELETE FROM yaf_User
        WHERE       BoardID = @BoardID
        DELETE FROM yaf_Rank
        WHERE       BoardID = @BoardID
        DELETE FROM yaf_Group
        WHERE       BoardID = @BoardID
        DELETE FROM yaf_AccessMask
        WHERE       BoardID = @BoardID
        DELETE FROM yaf_Active
        WHERE       BoardID = @BoardID
        DELETE FROM yaf_Board
        WHERE       BoardID = @BoardID
    END


